package com.jwthhealth_pub.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 处理日期
 * Created by liuzhen on 2017/12/1.
 */

public class DataUtil {
    /**
     * 6      * 时间戳转换成日期格式字符串
     * 7      * @param seconds 精确到秒的字符串
     * 8      * @param formatStr
     * 9      * @return
     * 10
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    public static String Data2TimeStamp(String time) {
        try {
            //Date或者String转化为时间戳
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String time="1970-01-06 11:45:55";
            Date date = format.parse(time);
            System.out.print("Format To times:"+date.getTime());
            return String.valueOf(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String Data2DayStamp(String time) {
        try {
            //Date或者String转化为时间戳
            SimpleDateFormat format =  new SimpleDateFormat("HH:mm:ss");
//            String time="1970-01-06 11:45:55";
            Date date = format.parse(time);
            System.out.print("Format To times:"+date.getTime());
            return String.valueOf(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断当前时间是否在目标时间段内, 日期格式 HH:mm
     * @param targetTime
     * @param startTime  example 6：00
     * @param endTime    example 6：00
     * @return
     */
    public static boolean containTime(String targetTime, String startTime, String endTime){
        long currentTimeMillis = System.currentTimeMillis();
        SimpleDateFormat format =  new SimpleDateFormat("HH:mm");
        try {
            Date currentData = format.parse(targetTime);
            Date begin = format.parse(startTime);
            Date end = format.parse(endTime);

            // TODO: 2017/12/12 不使用calendar是否可以
            Calendar currentT = Calendar.getInstance();
            Calendar beginT = Calendar.getInstance();
            Calendar endT = Calendar.getInstance();
            currentT.setTime(currentData);
            beginT.setTime(begin);
            endT.setTime(end);

            if (currentT.after(begin) && currentT.before(endT)) {
                return true;
            }

        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

        return false;
    }
}
